<?php

namespace App\Http\Controllers;

use App\Models\XunDianJiLu;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class XunDianGuanLiController extends Controller
{
    /**
     * 查询巡店数据
     *
     * @author Eric
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function xun_dian_list(Request $request){
        $params = $this->validate($request, [
            'shop_name' => 'nullable',
            'shop_code' => 'nullable',
            'user_name' => 'nullable',
            'created_at' => 'nullable',
        ]);

        // 分页数
        $paginate = $request->get('paginate', 10);

        $query = XunDianJiLu::query();
        $query->where('uid', Auth::id());
//        file_put_contents('巡店查询',);
//        return $params;
        foreach ($params as $key => $value) {
            if ($value != "" && $key != 'created_at') {
                $query->where($key, 'like', "%".$value."%");
            }

            if($key == 'created_at' && is_array($value)){
                $query->whereBetween(
                    'created_at',
                    [$value[0], date('Y-m-d', strtotime($value[1])).' 23:59:59']
                );
            }
        }

        return $this->success($query->orderBy('created_at','desc')->paginate($paginate));
    }

    /**
     * 巡店记录id查询明细
     *
     * @author Eric
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function xun_id_get_min_xi($id){

        $xunDianJiLu = XunDianJiLu::with('minxis')->where('id',$id)->first();

        $minxis =  $xunDianJiLu->minxis;

        if(count($minxis)){
            foreach ($minxis as $k => $v){
                switch ($v->task_type)
                {
                    case "select":
                        $v->task_type = "选择器";
                        break;
                    case "number":
                        $v->task_type = "数字";
                        break;
                    case "text":
                        $v->task_type = "文本";
                        break;
                    case "time":
                        $v->task_type = "日期";
                        break;
                }


            }
        }


        return $this->success($minxis);

    }
}
